/**
 * @param {number[][]} points
 * @param {number} k
 * @return {number[][]}
 */
export var kClosest = function (points, k) {
  const distances = points.map((p, i) => ({ p: p[0] ** 2 + p[1] ** 2, i }))
  distances.sort((a, b) => a.p - b.p)
  return distances.slice(0, k).map(d => points[d.i])
}

/**
 * @param {number[][]} points
 * @param {number} k
 * @return {number[][]}
 */
export var kClosest2 = function (points, k) {
  points.sort((a, b) => a[0] ** 2 + a[1] ** 2 - (b[0] ** 2 + b[1] ** 2))
  return points.slice(0, k)
}
